package com.ziyueyuwen.xiniao.controller.zyclass;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ao.ClassInfoAO;
import com.ziyueyuwen.xiniao.bean.ZyClassInfo;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.bean.vo.ZyClassVO;
import com.ziyueyuwen.xiniao.enums.ClassStatusEnum;
import com.ziyueyuwen.xiniao.enums.RoleEnum;
import com.ziyueyuwen.xiniao.service.ClassService;
import com.ziyueyuwen.xiniao.service.LessonService;
import com.ziyueyuwen.xiniao.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;

import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;
import static com.ziyueyuwen.xiniao.util.ConstUtil.PARAM_QUERY_DATA;

/**
 * @author wancheng  on 2018/12/17.
 */
@Validated
@Controller
@RequestMapping("/class")
public class ClassController {

    @Autowired
    ClassService classService;
    @Autowired
    LessonService lessonService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/save_class.do")
    public void saveClass(HttpServletRequest request,
                          HttpServletResponse response,
                          @Validated ClassInfoAO classInfoAO) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("添加班级");
        try {
            ZyClassInfo zyClassInfo = new ZyClassInfo();
            zyClassInfo.setClassName(classInfoAO.getClassName());
            zyClassInfo.setCourseId(classInfoAO.getCourseId());
            zyClassInfo.setStudentNum(classInfoAO.getStudentNum());
            zyClassInfo.setCreateTime(new Date());
            zyClassInfo.setStatus(ClassStatusEnum.CLASS_UN_START);
            classService.saveClass(zyClassInfo,classInfoAO.getMasterAccount(),classInfoAO.getTeacherAccount());
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_class.do")
    public void updateClass(HttpServletRequest request,
                            HttpServletResponse response,
                            Integer classId,
                            String className) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改班级名称");
        try {
            classService.updateClass(classId, className, null,null);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/delete_class.do")
    public void updateClass(HttpServletRequest request,
                            HttpServletResponse response,
                            Integer classId) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("删除班级");
        try {
            String userAccount = hrc.getUserInfo().getUserAccount();
            classService.deleteClass(classId,userAccount);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_my_class.do")
    public void getMyClass(HttpServletRequest request,
                           HttpServletResponse response,
                           ClassStatusEnum status,
                           @RequestParam(value = PARAM_QUERY_DATA, required = false) String queryData,
                           Integer page,
                           Integer rows) {
        HttpResContent<PageData<ZyClassVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("我的班级列表");
        try {
            String userAccount = hrc.getUserInfo().getUserAccount();
            if (roleService.ifHasRole(userAccount, RoleEnum.ADMIN)){
                userAccount = null;
            }
            PageData<ZyClassVO> res = classService.getByUserAccount(userAccount, status, queryData, page, rows);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_class_by_course_id.do")
    public void getClassInfoByCourseId(HttpServletRequest request,
                                       HttpServletResponse response,
                                       Integer courseId,
                                       Integer page,
                                       Integer rows) {
        HttpResContent<PageData<ZyClassVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取班级列表");
        try {
            PageData<ZyClassVO> res = classService.getByCourseId(courseId, page, rows);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }


    @RequestMapping(value = "/get_class_info_by_id.do")
    public void getClassInfoById(HttpServletRequest request,
                                 HttpServletResponse response,
                                 Integer classId) {
        HttpResContent<ZyClassVO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取班级详情");
        try {
            ZyClassVO res = classService.getClassInfoByClassId(classId);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

}
